package com.jdew.thkj.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.OrderBean;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2018/6/26.
 */

public class OrderDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List<OrderBean.DataBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;

    public OrderDetailAdapter(Context context, List<OrderBean.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setDataList(List<OrderBean.DataBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void setDataAllList(List<OrderBean.DataBean> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = layoutInflater.inflate(R.layout.order_detail_item, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myviewHolder = (MyViewHolder) holder;
        myviewHolder.BindView(holder, position);
        myviewHolder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.pic)
        ImageView pic;
        @Bind(R.id.ptitle)
        TextView ptitle;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.pay)
        TextView pay;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.creat)
        TextView creat;
        @Bind(R.id.end)
        TextView end;

        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void BindView(RecyclerView.ViewHolder holder, int position) {
             DecimalFormat df = new DecimalFormat("0.00");
            ((MyViewHolder) holder).ptitle.setText(dataList.get(position).getGoods_name());
            ((MyViewHolder) holder).name.setText(dataList.get(position).getShop());
            ((MyViewHolder) holder).pay.setText("¥ " + df.format(Double.parseDouble(dataList.get(position).getOrder_amount())));
            ((MyViewHolder) holder).price.setText("¥ " + df.format(Double.parseDouble(dataList.get(position).getGoods_price())));
            ((MyViewHolder) holder).creat.setText("创建时间 "+dataList.get(position).getClick_time().replace("T"," "));
            ((MyViewHolder) holder).end.setText("付款时间 "+dataList.get(position).getCreate_time().replace("T"," "));
            ((MyViewHolder) holder).num.setText("x"+dataList.get(position).getGoods_number());
             Glide.with(context).load(dataList.get(position).getAdv_name()).apply(new RequestOptions().placeholder(R.mipmap.xiajia).error(R.mipmap.xiajia).dontAnimate().centerCrop()).into(pic);
        }
    }
}

